﻿using DexComanda.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DexComanda
{
  
   public  static class Program
    {

       
     
       /// <summary>
        /// 
        /// 
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
     public static void Main()
        {

            var autoConfig = new AutoConfig();
            var temp = Directory.GetCurrentDirectory() + @"\ConnectionString_DexComanda.txt";
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

          

            if (!File.Exists(temp))
            {
                Application.Run(new frmConfiguracoes());
            
            } else {

                StreamReader tempDex = new StreamReader(temp);

                string sLine = "";
                ArrayList arrText = new ArrayList ();

                sLine = tempDex.ReadLine();

                Conexao.connectionString = sLine;

                Conexao con = new Conexao();

                var banco = con.SelectAll("Empresa", "spObterEmpresa");

                if ((banco.Tables["Empresa"].Rows.Count == 0))
                {
                    MessageBox.Show("Não Há empresas Cadastradas");     
                }
                else
                {
                    DataSet Empresa = con.SelectAll("Empresa", "spObterEmpresa");

                    DataRow dRow = Empresa.Tables["Empresa"].Rows[0];

                    Empresa empresa = new Empresa
                    {
                        Codigo = int.Parse(dRow.ItemArray.GetValue(0).ToString()),
                        Nome = dRow.ItemArray.GetValue(1).ToString(),
                        CNPJ = dRow.ItemArray.GetValue(2).ToString(),
                        Telefone = dRow.ItemArray.GetValue(3).ToString(),
                        Telefone2 = dRow.ItemArray.GetValue(4).ToString(),
                        Endereco = dRow.ItemArray.GetValue(5).ToString(),
                        Cep = int.Parse(dRow.ItemArray.GetValue(6).ToString()),
                        Cidade = dRow.ItemArray.GetValue(7).ToString(),
                        Numero = int.Parse(dRow.ItemArray.GetValue(8).ToString()),
                        Bairro = dRow.ItemArray.GetValue(9).ToString(),
                        UF = dRow.ItemArray.GetValue(10).ToString(),
                        PontoReferencia = dRow.ItemArray.GetValue(11).ToString(),
                        Banco = dRow.ItemArray.GetValue(12).ToString(),
                        Contato = dRow.ItemArray.GetValue(13).ToString(),
                        Email = dRow.ItemArray.GetValue(14).ToString(),
                        Servidor = dRow.ItemArray.GetValue(15).ToString(),
                        TipoEmpresa = int.Parse(dRow.ItemArray.GetValue(17).ToString()),
                        VersaoBanco = dRow.ItemArray.GetValue(18).ToString(),
                      //  VersaoBanco =int.Parse(dRow.ItemArray.GetValue(15).ToString())                        
                    };

                    autoConfig.setEmpresa(empresa);

                }

                if (AutoConfig.getEmpresa().TipoEmpresa == 1)
                {
                    Application.Run(new Main());
                }
                else if (AutoConfig.getEmpresa().TipoEmpresa == 2)
                {
                    Application.Run(new DexPDV());
                }
                else
                {
                    Application.Run(new Main());
                }
                
            }

        }
 
    }
}
